package com.gimi.cloud.bbp.manage.service.page.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gimi.cloud.bbp.common.utils.MyBeanUtils;
import com.gimi.cloud.bbp.dao.dao.page.PageTemplateDao;
import com.gimi.cloud.bbp.dto.page.PageTemplateDTO;
import com.gimi.cloud.bbp.entity.page.PageTemplate;
import com.gimi.cloud.bbp.manage.service.page.PageTemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class PageTemplateServiceImpl implements PageTemplateService{

	@Autowired private PageTemplateDao pageTemplateDao;
	
	@Override
	@Transactional
	public PageTemplate saveInfo(PageTemplateDTO dto) { 
		PageTemplate entity = null;
		if (dto.getId() != null) {
			entity = pageTemplateDao.queryBy("id", dto.getId());
		}
		if (entity != null) {
			MyBeanUtils.copyPropertiesIgnoreNullAndEmptyStr(dto, entity);
			pageTemplateDao.baseUpdateByPrimaryKey(entity);
		} else {
			entity = new PageTemplate();
			MyBeanUtils.copyPropertiesIgnoreNullAndEmptyStr(dto, entity);
			pageTemplateDao.baseInsert(entity);
		}
		return entity;
	}
	
	@Override
	public PageInfo<PageTemplate> getList(PageTemplateDTO dto){
		Example example = new Example(PageTemplate.class);
		example.setOrderByClause("id desc");
		if (dto.getName() != null) {
			Criteria criteria = example.createCriteria();
			criteria.andLike("name", "%"+ dto.getName() + "%");
		}
		PageHelper.startPage(dto.getPage(), dto.getColumn());
		List<PageTemplate> entitys = pageTemplateDao.selectByExample(example);
		return new PageInfo<>(entitys);
	}
	
	@Override
	@Transactional
	public Long delInfo(Integer... ids){
		if (ids == null || ids.length < 1) {
			return 0L;
		}
		return pageTemplateDao.delById(StringUtils.join(ids, ","));
	}
	
	@Override
	public PageTemplate getInfoById(Integer id) {
		return pageTemplateDao.selectByPrimaryKey(id);
	}
	
}
